package approvalDaoImplement;

import approvalDao.ConnectionFactory;
import approvalDao.Information;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.*;

@WebServlet(name = "BorrowServlet", value = "/BorrowServlet")
public class BorrowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /*
        使用InputStreamReader对象，获取前端传来的数据.其中
        request.getInputStream()是读取前端传递来的数据字节流，
        StandardCharsets.UTF_8是将前端传来的数据转化为UTF-8的编码方式
         */

        InputStreamReader inputStreamReader = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        PrintWriter out=response.getWriter();

        StringBuilder str = new StringBuilder();
        int respInt = inputStreamReader.read();
        while (respInt!=-1){
            str.append((char) respInt);
            respInt = inputStreamReader.read();
        }

        //将json数据转化为自定义的Java自定义对象
        String jsonString =String.valueOf(str);

        JSONObject jsonObject = JSON.parseObject(jsonString);
        Information information = JSONObject.toJavaObject(jsonObject,Information.class);


        String choice = information.getChoice();
        String bookName = information.getBookName();
        String userName = information.getUserName();
        String borrowDate = information.getBorrowDate();
        String backDate = information.getBackDate();
        String  Email = information.getEmail();
        String sta = Information.UpdateStatus(choice);


      /*
      测试
        out.write(jsonString);
        out.write(backDate);
*/

        //更新状态


        Connection connection = ConnectionFactory.getConnection();
        Statement statement = null;
        String sql = "UPDATE `borrowrecord` SET status =sta WHERE username = "+userName + "AND email = " + Email;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }




        /*
        更新状态并写入历史记录数据表BorrowRecord

        Connection connection = ConnectionFactory.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "INSERT INTO borrowrecord(bookName,userName,borrowDate,backDate,phone,status)VALUES(?,?,?,?,?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,bookName);
            preparedStatement.setString(2,userName);
            preparedStatement.setString(3,borrowDate);
            preparedStatement.setString(4,backDate);
            preparedStatement.setString(5,email);
            preparedStatement.setString(6,sta);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        //关闭连接
        /*
        try {
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        */

        request.getRequestDispatcher("FeedbackServlet").forward(request,response);



    }
}
